using StockTradTest;
using StockTradTest.Data;
using StockTradTest.strategy;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using System.Threading;
namespace Test
{
    class Program
    {
        static void Main()
        {
            while(true)
            {
                Console.WriteLine("请输入需要完成的任务:");
                Console.WriteLine("1.更新股票列表信息  2.更新所有股票信息  3.更新单只股票信息  4.更新投资者数据    5.更新所有指数信息");
                Console.WriteLine("6.查询股票历史信息  7.查询股票实时信息  8.查询指数历史信息  9.查询指数实时信息  Q.查询账户信息");
                Console.WriteLine("R.执行测试系统      C.初始化账户信息    D.删除所有数据库信息 T.查询多只股票实时信息滚动  0.跑路");
                Console.WriteLine("O.综合大测试        M.多只股票测试");
                string getNum = Console.ReadKey().KeyChar.ToString();
                switch (getNum)
                {
                    case "0":return;
                    case "1": Funciton.UpdateInfo().Wait();break;
                    case "2": UpdateAllStock();break;
                    case "3":UpdateStock();break;
                    case "4": Console.WriteLine("投资者数据更新中"); GetInvestor.AutoSaveInvestorsAsync().Wait();Console.WriteLine("投资者数据更新完成"); break;
                    case "5": Funciton.UpdateAllIndexAsync().Wait();break;
                    case "6": QueryStockHistory(); break;
                    case "7": QueryRealTimeStockInfo(); break;
                    case "8": QueryIndexHistory(); break;
                    case "9": QueryRealTimeIndexInfo(); break;
                    case "Q":QueryMyAccountInfo();break;
                    case "R": TestAsync().Wait();break;
                    case "C": ResetMyAccount(); break;
                    case "D": DeleteAllDataBase(); break;
                    case "T": QueryingRealTimeStockInfo(); break;
                    case "O": BigTest(); break;
                    case "M": MultiTest(); break;
                    default:Console.WriteLine("输入错误，请重新输入"); break;
                }

            }

        }
        //批量测试
        static void BigTest()
        {
            List<StockInfo> stockInfos = Base.StockInfoDatabase.GetAllStockInfoAsync().Result;
            double[] funds = new double[2];
            double max = 0;
            funds = funds.Select(data => 0d).ToArray();
            
            TextRecord.Record("Biggest test Start:", true);
            Console.WriteLine("Start");
            int win = 0;
            int lose = 0;
            //考虑到数据操作问题，因只有一个账户文件，为保持逻辑统一，并行计算方式在此处不可用
            foreach (StockInfo info in stockInfos)
            {
                if (!info.Name.Contains("银行")) continue;
                Funciton.UpdateStockDay(info.Code).Wait()                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    ;
                funds[0] = MyStrategy.ShortBase(info.Code);
                funds[1] = MyStrategy.TradeShortMiddleTest(info.Code, 20);

                if(funds[0]<funds[1] & funds[1] > 500000)
                {
                    TextRecord.Record(info.Code + "胜原始500000,最终" + funds[1] + "基准" + funds[0]);
                    win++;
                }
                else
                {
                    TextRecord.Record(info.Code + "败原始500000,最终" + funds[1] + "基准" + funds[0]);
                    lose++;
                }
            }
            TextRecord.Record(string.Format("测试胜{0}败{1}，胜率={2}%",win,lose,(double)win/(win+lose)*100));
            TextRecord.OpenRecord();


        }
        //单只股票实时测试
        static async Task TestAsync()
        {
            Console.Write("请输入股票代码（默认601166请按回车）:");
            string code = Console.ReadLine();
            if (string.IsNullOrEmpty(code)) code = "601166";
            while (code.Length != 6)
            {
                Console.WriteLine("错误输入!请重新输入:");

                code = Console.ReadLine();
            }
            Console.WriteLine("开始测试");
            TestTradBase.isOnClound = true;
            await MyStrategy.TradeAberationRealAllAsync(code, 20);

        }
        //多只股票实时测试
        static void MultiTest()
        {

            Console.WriteLine("开始测试");
            MyStrategy.TradeMultiAberrationTest( 10, true);
        }

        static void UpdateAllStock()
        {
            Console.Write("是否更新已存在的数据(Y确定,其他任意键否):");
            if (Console.ReadKey().KeyChar.ToString() == "Y") 
                Funciton.UpdateAllAsync(false,true).Wait();
            else
                Funciton.UpdateAllAsync(false).Wait();
        }
        static void UpdateStock()
        {
            Console.Write("请输入股票代码:");
            string code = Console.ReadLine();
            Funciton.UpdateStockDay(code).Wait();
            Console.Write("更新完成");
            Console.WriteLine(Funciton.QueryStockInfo(code, 0));
            Console.WriteLine(Funciton.QueryStockInfo(code, 1));
        }

        static void QueryStockHistory()
        {
            Console.Write("请输入股票代码:");
            string code = Console.ReadLine();
            Console.Write("请输入日期(0开始日期 1最新日期):");
            int.TryParse(Console.ReadLine(), out int date);
            Console.WriteLine(Funciton.QueryStockInfo(code, date));
        }
        static void QueryIndexHistory()
        {
            Console.Write("请输入指数代码:");
            string code = Console.ReadLine();
            Console.Write("请输入日期(0开始日期 1最新日期):");
            int.TryParse(Console.ReadLine(), out int date);
            Console.WriteLine(Funciton.QueryIndexInfo(code, date));
        }

        static void QueryRealTimeStockInfo()
        {
            Console.Write("请输入股票代码:");
            string code = Console.ReadLine();
            Console.WriteLine(Funciton.QueryRealTimeStockInfo(code));
        }

        static void QueryingRealTimeStockInfo()
        {
            Console.Write("请输入股票只数:");
            int.TryParse(Console.ReadLine(),out int num);
            string[] codes = new string[num];
            for(int i = 0;i < num;i++)
            {
                Console.Write(string.Format("请输入第{0}只股票代码:",i+1));
                codes[i] = Console.ReadLine();
            }
            Console.Clear();
            Console.CursorVisible = false;
            bool open = true;
            while (open)
            {
                for (int i = 0; i < num; i++)
                {
                    Console.WriteLine(Funciton.QueryRealTimeStockInfo(codes[i]));
                }
                Console.SetCursorPosition(0, 0);
                ConsoleKey key = new ConsoleKey();
                Task.Factory.StartNew(() => { key = Console.ReadKey().Key; }).Wait(1 * 1000);
                if (key == ConsoleKey.Escape)
                {
                    open = false;
                    Console.Clear();
                    Console.CursorVisible = true;
                }
            }
            
        }

        static void QueryRealTimeIndexInfo()
        {
            Console.Write("请输入指数代码:");
            string code = Console.ReadLine();
            Console.WriteLine(Funciton.QueryRealTimeIndexInfo(code));
        }
        static void QueryMyAccountInfo()
        {
            Console.WriteLine(Funciton.QueryMyAccountInfo());
        }
        static void ResetMyAccount()
        {
            if(Funciton.ResetMyAccount())
            {
                Console.WriteLine("账户初始化成功");

            }
            QueryMyAccountInfo();
        }
        static void DeleteAllDataBase()
        {
            Console.Write("确定要删除吗(Y删除,其他任意键放弃):");
            if(Console.ReadKey().KeyChar.ToString() == "Y")
            {
                Directory.Delete(Base.Root,true);
                Console.WriteLine("删除成功");
            }
            else
            {
                Console.WriteLine("放弃删除");
            }

        }
    }
}
